rm(list=ls())
require(foreign)
require(ebal)
require(xtable)
require(stargazer)
require(arm)
require(plm)
library(car)
library(dplyr)
library(plm)
library(plyr)
library(Zelig)
library(mvtnorm)
library(MatchIt)
library (cem)
library(MatchingFrontier)


#Table 1 Replication - Refer to STATA File
#Table 2 Replication - Below

#Step 1 - put together data. Read Rory's Data and Merge with our Coastal File
setwd("~/Dropbox/Classes/Spring 2015/Replication Paper/Final - To Turn In/Code/R Analysis")
trim.panel <- read.dta ("trim.panel.3.dta")
trim.cs <- read.dta ("trim2.dta")
trim.cs2 <-select(trim.cs,gvkey,city,npc,coastal,price2007,roa2007,roa2006,roa2005,coastal,margin2007,margin2006,
                  margin2005,rev2007,rev2006,rev2005,so_portion,debtratio2007,shares2007,txpd2007,firmage)
trim.cs3 <- merge(trim.cs2,trim.panel,by="gvkey")
trim.cs4 <-select(trim.cs3,gvkey,city.y,npc.y,price2007,roa2007.y,roa2006.y,roa2005.y,
                  coastal.y,margin2007.y,margin2006.y,margin2005.y,rev2007.y,rev2006.y,rev2005.y,
                  so_portion.y,debtratio2007,shares2007,txpd2007,firmage,roa2006.y,
                  margin2006.y,roa2007.y,margin2007.y,roa2008,margin2008,roa2009,
                  margin2009,roa2010,margin2010,roa,fyear_ind1,fyear_ind2,fyear_ind3,fyear_ind4,fyear_ind5,fyear_ind6,
                  fyear)

#Match on Relevant Factors Including Coastal
trim.match <- matchit(formula = npc ~ price2007 + roa2007 + roa2006 + roa2005 + coastal+
                        margin2007+ margin2006+ margin2005+ rev2007+ rev2006 + rev2005 +
                        so_portion+debtratio2007+shares2007+txpd2007+firmage,
                      data = trim.cs2,method = "nearest",
                      distance = "logit",
                      discard="control")

data.matched <-match.data(trim.match)

#Match on Relevant Factors without Coastal or SO Portion
trim.match.second <- matchit(formula = npc ~ price2007 + roa2007 + roa2006 + roa2005 +
                              margin2007+ margin2006+ margin2005+ rev2007+ rev2006 + rev2005 +
                              debtratio2007+shares2007+txpd2007+firmage,
                            data = trim.cs2,method = "nearest",
                            distance = "logit",
                            discard="control")

data.matched.second <-match.data(trim.match.second)

#Match on Relevant Factors without Coastal
trim.match.third <- matchit(formula = npc ~ price2007 + roa2007 + roa2006 + roa2005 +
                               margin2007+ margin2006+ margin2005+ rev2007+ rev2006 + rev2005 +
                               so_portion+debtratio2007+shares2007+txpd2007+firmage,
                             data = trim.cs2,method = "nearest",
                             distance = "logit",
                             discard="control")

data.matched.third <-match.data(trim.match.third)


#Subset Panel Data for Matched Observations from Above
trim.panel.short <- subset(trim.panel,trim.panel$gvkey %in% data.matched$gvkey)
trim.panel.short.second <-subset(trim.panel,trim.panel$gvkey %in% data.matched.second$gvkey)
trim.panel.short.third <-subset(trim.panel,trim.panel$gvkey %in% data.matched.third$gvkey)

#Pull 2007 Data since we only want 07 data for matching
nomatch.2007 <- filter(trim.cs3,fyear==2007)
nomatch.2007$npc <- nomatch.2007$npc.x

#2008 Data - relevant later only to assign NPC membership (NPC membership not coded in 2007 set)
trim.cs08 <- filter(trim.cs3,fyear==2008)

#TABLE 2
regression1<- lm(roa~npc+coastal+ as.factor(fyear)+as.factor(gvkey),data=trim.panel.short)
summary(regression1)

regression2<- lm(margin~npc+coastal+ as.factor(fyear)+as.factor(gvkey),data=trim.panel.short)
summary(regression2)

regression3<- lm(roa~npc+as.factor(coastal)+ as.factor(fyear)+as.factor(gvkey),data=trim.panel.short.third)
summary(regression3)

regression4<- lm(margin~npc+ as.factor(fyear)+as.factor(gvkey),data=trim.panel.short.third)
summary(regression4)

#TABLE 3
nomatch07.so <-  zelig(npc ~so_portion.y,data=nomatch.2007,model="logit")
summary(nomatch07.so)

#FIGURE 1
so1 <- setx(nomatch07.so, so_portion.y=0:1)
socheck <- sim(nomatch07.so, x = so1)
plot(socheck,xlab="State Owned Portion",ylab="Expected Probability of NPC Membership")

#TABLE 4
trim.coastal.fixed.no.so <-  zelig(npc.y ~coastal.y +price2007 + roa2007.x + roa2006.x + roa2005.x +
                                     margin2007.x+ margin2006.x+ margin2005.x+ rev2007.x+ rev2006.x + rev2005.x +
                                     debtratio2007+shares2007+txpd2007+firmage,data=trim.cs08,model="logit")

summary(trim.coastal.fixed.no.so)


#FIGURE 2
#Regression Identifying Coastal
trim.coastal.fixed.no.so <-  zelig(npc.y ~coastal.y +price2007 + roa2007.x + roa2006.x + roa2005.x +
                                     margin2007.x+ margin2006.x+ margin2005.x+ rev2007.x+ rev2006.x + rev2005.x +
                                     debtratio2007+shares2007+txpd2007+firmage,data=trim.cs08,model="logit")

#Checking What Value of Coastal Would Be if =1 or =0
coastal1 <- setx(trim.coastal.fixed.no.so, coastal.y=1)
coastal0 <- setx(trim.coastal.fixed.no.so, coastal.y=0)
coastalcheck <- sim(trim.coastal.fixed.no.so, x = coastal1, x1 = coastal0)
summary(coastalcheck)
plot(coastalcheck)


